//
//  ViewController.m
//  UIDynamicTest
//
//  Created by wangrui on 14-2-13.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UIView *testView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.testView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.testView.backgroundColor = [UIColor blueColor];
    self.testView.transform = CGAffineTransformRotate(self.testView.transform, 45);
    [self.view addSubview:self.testView];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.testView addGestureRecognizer:pan];
    
    //referenceView 相当于一个力学空间
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    
    //behavior给item 添加物体力学属性
    UIGravityBehavior *b = [[UIGravityBehavior alloc] init];
    [b setMagnitude:5];
    [b addItem:self.testView];
    
    UICollisionBehavior *c = [[UICollisionBehavior alloc]init];
    c.collisionMode = UICollisionBehaviorModeBoundaries;
    c.translatesReferenceBoundsIntoBoundary = YES;
    [c addItem:self.testView];
    
    UIDynamicItemBehavior *d = [[UIDynamicItemBehavior alloc]initWithItems:@[self.testView]];
    d.elasticity = 0.5;

    [self.animator addBehavior:b];
    [self.animator addBehavior:c];
    [self.animator addBehavior:d];
}

- (void)pan:(UIGestureRecognizer *)g{
    self.testView.center = [g locationInView:self.view];

    if (g.state == UIGestureRecognizerStateEnded) {
        [self.animator updateItemUsingCurrentState:self.testView];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
